package com.teach;

/**
 * <pre>
 * - 
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-4-15 9:12:37
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 *
 */
public class Demo04 {

	public static void main(String[] args) {
		System.out.println(toLowerCamelCase("USER_NAME"));
		System.out.println(toLowerCamelCase("product_manage_page"));
		System.out.println(toUpperCamelCase("USER_NAME"));
		System.out.println(toUpperCamelCase("product_manage_page"));
		System.out.println(toUnderlineCase("UserName"));
		System.out.println(toUnderlineCase("ProductManagePage"));
	}
	
	public static String toUpperCamelCase(String str) {
		return toCamelCase(str, true);
	}
	
	public static String toLowerCamelCase(String str) {
		return toCamelCase(str, false);
	}
	
	public static String toCamelCase(String str,boolean upper) {
		StringBuffer sb = new StringBuffer();
		char[] cs = str.toCharArray();
		for(char c : cs) {
			if(c=='_') {
				upper = true;
				continue;
			}
			if(upper) {
				sb.append(Character.toUpperCase(c));
				upper = false;
			}else {
				sb.append(Character.toLowerCase(c));
			}
			
		}
		return sb.toString();
	}
	
	public static String toUnderlineCase(String str) {
//		StringBuffer sb = new StringBuffer();
//		char[] cs = str.toCharArray();
//		for(char c : cs) {
//			if(Character.isUpperCase(c)) {
//				sb.append("_");
//			}
//			sb.append(Character.toUpperCase(c));
//		}
//		if(sb.charAt(0)=='_') {
//			sb.delete(0, 1);
//		}
//		return sb.toString();
		str = str.substring(0, 1).toLowerCase()+str.substring(1);
		return str.replaceAll("([A-Z])", "_$1").toUpperCase();
	}
}
